const CFG = require("../config");

// 为Data对象补充一个属性
Date.prototype.format = function(format) {
  // eg:format="YYYY-MM-dd hh:mm:ss";
  var o = {
    "M+" :this.getMonth() + 1, // month
    "d+" :this.getDate(), // day
    "h+" :this.getHours(), // hour
    "m+" :this.getMinutes(), // minute
    "s+" :this.getSeconds(), // second
    "q+" :Math.floor((this.getMonth() + 3) / 3), // quarter
    "S" :this.getMilliseconds()  // millisecond
  }
  if (/(y+)/.test(format)) {
    format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  }
  for ( var k in o) {
    if (new RegExp("(" + k + ")").test(format)) {
      format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]
        : ("00" + o[k]).substr(("" + o[k]).length));
    }
  }
  return format;
}

//let moment = require('moment');

//以小时取整
function HourRounding(ms=0)
{
  if(ms==0) ms=(Date.now()+CFG.SRV_TIMEZONE_OFFSET_MS);
  ms=~~(ms/3600000);
  return ms*3600000;
}

//以十分钟取整
function TenMinRounding(ms=0)
{
  if(ms==0) ms=(Date.now()+CFG.SRV_TIMEZONE_OFFSET_MS);
  ms=~~(ms/600000);
  return ms*600000;
}

//以unit_s秒对ms取整
function Rounding(unit_s,ms=0)
{
  if(ms==0) ms=(Date.now()+CFG.SRV_TIMEZONE_OFFSET_MS);
  let sec=~~(ms/1000);
  sec+=(3600*8);//时区矫正
  let tail=sec%unit_s;
  sec-=(3600*8);////时区矫正
  return (sec-tail)*1000;
}

//前一个取整数
//返回取整结果再往前走一格的时间戳
function PrevRounding(unit_s,ms=0)
{
  if(ms==0) ms=(Date.now()+CFG.SRV_TIMEZONE_OFFSET_MS);
  let sec=~~(ms/1000);
  sec-=unit_s;//往前走一个单位
  sec+=(3600*8);//时区矫正
  let tail=sec%unit_s;
  sec-=(3600*8);////时区矫正
  return (sec-tail)*1000;
}

//null 当前日期时间字符串
//!=null 将时间戳转换为时间字符串
function Stamp2Str(stamp = null)
{
  if(stamp == null) stamp = new Date(Date.now()+CFG.SRV_TIMEZONE_OFFSET_MS);
  else stamp = new Date(stamp+CFG.SRV_TIMEZONE_OFFSET_MS);

  let dateString = stamp.toLocaleString('en-US', {hour12:false});
  return dateString.replace(/\s24:/g,' 00:'); //.match(/[0-9:-\s]{1,}/)[0];
}

// null 返回今天的零点时间
// !=null 返回对应天的零点时间
function Stamp2Zero(stamp = null) {
  if (stamp == null) stamp = new Date(Date.now()+CFG.SRV_TIMEZONE_OFFSET_MS);
  else stamp = new Date(stamp+CFG.SRV_TIMEZONE_OFFSET_MS);

  stamp.setHours(0, 0, 0, 0);
  return stamp.getTime();
}

//null 当前毫秒时间戳
//!=null 将时间字符串转换为时间戳
function Str2Stamp(str=null)
{
  if (str == null) return Date.now() + CFG.SRV_TIMEZONE_OFFSET_MS;
  return Date.parse(str);
}

//当前日期偏移
//返回当前日期偏移后的字符串
function CurrDateStr(offsetDay)
{
  let sec = Rounding(24*3600, NowStamp()+offsetDay*24*3600*1000);
  return Stamp2Str(sec);//.replace(/\//g,'-');//替换斜杠为横杠
}

// 获取当前小时分钟，比如8:38，将得到838
function CurrHourMinute()
{
  let d = new Date();
  return (d.getHours() + CFG.SRV_TIMEZONE_OFFSET) * 100 + d.getMinutes();
}

// 获取当前小时
function CurrHour()
{
  let d = new Date();
  return d.getHours() + CFG.SRV_TIMEZONE_OFFSET;
}

// 获取当前年月日
function CurrYearMonthDay()
{
  let d = new Date();
  return d.getFullYear()*10000 + (d.getMonth()+1)*100 + d.getDate();
}

// 获取当前时间的格式化数字字符串
function CurrTimeNum(fmt)
{
  let d = new Date();
  return d.format(fmt);
}

// 时间戳转格式化数字字符串
function StampToTimeNum(stamp, fmt)
{
  let d = new Date(parseInt(stamp));
  return d.format(fmt);
}

// 判断当前时间是否在时间段数组字符串内
// timeStr: 类似'929:30-1130,1300-1500'
// nowTimNum：hhmmss格式，默认当前时间
function NowTimeInScope(timeStr, nowTimNum=null)
{
  if (!timeStr) return false;

  let timeArr = timeStr.split(',');
  if (timeArr.length == 1) timeArr = timeStr.split(';'); // 补充对分号分隔的支持
  if (nowTimNum == null) nowTimNum = INT(CurrTimeNum('hhmmss'));

  for (let t of timeArr) {
    if (t == '*') return true; // *代表不限制

    let tt = t.split('-');
    if (tt.length < 2) return false; // 必须有起点终点
    let start = tt[0].split(':'); // 看看含不含秒
    let end = tt[1].split(':');

    if (start.length >= 2) start = SUM_I(start[0] * 100, start[1]);
    else start = INT(start[0] * 100);

    if (end.length >= 2) end = SUM_I(end[0] * 100, end[1]);
    else end = INT(end[0] * 100);

    if (nowTimNum >= start && nowTimNum <= end) return true;
  }
  return false;
}

exports.HourRounding = HourRounding;
exports.TenMinRounding = TenMinRounding;
exports.Rounding = Rounding;
exports.PrevRounding = PrevRounding;
exports.Stamp2Str = Stamp2Str;
exports.Stamp2Zero = Stamp2Zero;
exports.NowStr = Stamp2Str;
exports.Str2Stamp = Str2Stamp;
exports.NowStamp = Str2Stamp;
exports.CurrDateStr = CurrDateStr;
exports.CurrHourMinute = CurrHourMinute;
exports.CurrHour = CurrHour;
exports.CurrTimeNum = CurrTimeNum;
exports.StampToTimeNum = StampToTimeNum;
exports.CurrYearMonthDay = CurrYearMonthDay;
exports.NowTimeInScope = NowTimeInScope;

//let stamp=Date.parse('2020-11-10 20:11:15');//从时间字符串转换到时间戳
//let nowStamp=Date.now();//获取当前时间戳
//let dateString=new Date();//获取当前日期时间字符串
//let dateString=new Date().toLocaleString('chinese',{hour12:false});//易读的24进制的当地时间字符串
//let dateString=new Date(1605010200000);//从时间戳创造时间对象，输出时间字符串，注意时区